<html>
<head>
  <script src="../../OLLoader.js"></script>
  <script type="text/javascript">

    function test_initialize(t) {
        t.plan(1);

        var format = new OpenLayers.Format.WFST.v1_0_0({});
        t.ok(format instanceof OpenLayers.Format.WFST.v1_0_0, "constructor returns instance");
    }
    
    function test_read(t) {
        t.plan(2);
        
        var data = readXML("Transaction_Response");        
        var format = new OpenLayers.Format.WFST.v1_0_0({
            featureNS: "http://www.openplans.org/topp",
            featureType: "states"
        });
        var result = format.read(data);
        t.eq(result.insertIds[0], "none", "InsertIds read correctly");
        t.eq(result.success, true, "Success read correctly");
    }
    
    function test_write(t) {

        var format = new OpenLayers.Format.WFST.v1_0_0({
            featureNS: "http://www.openplans.org/topp",
            featureType: "states",
            featurePrefix: "topp",
            geometryName: "the_geom"
        });

        var cases = [{
            id: "query0",
            writer: "wfs:Query",
            arg: {
                filter: new OpenLayers.Filter.Spatial({
                    type: OpenLayers.Filter.Spatial.BBOX,
                    value: new OpenLayers.Bounds (1,2,3,4)
                })
            }
        }, {
            id: "query1",
            writer: "wfs:Query",
            arg: {
                srsNameInQuery: true,
                srsName: "EPSG:900913"
            }
        }, {
            id: "getfeature0",
            writer: "wfs:GetFeature",
            arg: {
                propertyNames: ["STATE_NAME", "STATE_FIPS", "STATE_ABBR"]
            }
        }];
        
        t.plan(cases.length);
        
        var test, got, exp;
        for(var i=0; i<cases.length; ++i) {
            test = cases[i];
            exp = readXML(test.id);
            got = format.writeNode(test.writer, test.arg);
            t.xml_eq(got, exp, test.id + ": correct request");
        }

    }
    
    function test_write_poorconfig(t) {
        t.plan(1);
        var format = new OpenLayers.Format.WFST.v1_0_0({
            featureType: "states",
            featurePrefix: "topp"
        });
        var exp = "topp:states";
        var got = format.writeNode("wfs:Query").getAttribute("typeName");
        t.eq(got, exp, "Query without featureNS but with featurePrefix queries for the correct featureType");
    }

    var xmlFormat = new OpenLayers.Format.XML();
    function readXML(id) {
        var xml = document.getElementById(id).firstChild.nodeValue;
        return xmlFormat.read(xml).documentElement;        
    }

  </script>
</head>
<body>
<div id="Transaction_Response"><!--
<wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc">
    <wfs:InsertResult>
        <ogc:FeatureId fid="none"/>
    </wfs:InsertResult>
    <wfs:TransactionResult>
        <wfs:Status>
            <wfs:SUCCESS/>
        </wfs:Status>
    </wfs:TransactionResult>
</wfs:WFS_TransactionResponse>
--></div>
<div id="query0"><!--
<wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
    <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
        <ogc:BBOX>
            <ogc:PropertyName>the_geom</ogc:PropertyName>
            <gml:Box xmlns:gml="http://www.opengis.net/gml">
                <gml:coordinates decimal="." cs="," ts=" ">1,2 3,4</gml:coordinates>
            </gml:Box>
        </ogc:BBOX>
    </ogc:Filter>
</wfs:Query>
--></div>
<div id="query1"><!--
<wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" srsName="EPSG:900913" xmlns:topp="http://www.openplans.org/topp">
</wfs:Query>
--></div>
<div id="getfeature0"><!--
<wfs:GetFeature service="WFS" version="1.0.0" xmlns:topp="http://www.openplans.org/topp"
                xmlns:wfs="http://www.opengis.net/wfs"
                xmlns:ogc="http://www.opengis.net/ogc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd">
    <wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
        <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
        <ogc:PropertyName>STATE_FIPS</ogc:PropertyName>
        <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
    </wfs:Query>
</wfs:GetFeature>
--></div>
</body>
</html>
